﻿using System.Data.SQLite;
using DVDTek_Data_Object.Accessor;

namespace DVDTek_Data_Object.DataObject
{
    public class BorrowerDataObject
    {
        #region Members
        private int id;
        private string first_name;
        private string last_name;

        public int Id
        {
            get { return this.id; }
        }
        public string FirstName
        {
            get { return this.first_name; }
            set
            {
                BorrowerDataObject borrower = BorrowerAccessor.Instance.Select(value, this.LastName);
                if ((borrower == null) || (borrower.Id == this.Id))
                    this.first_name = value;
                else if (this.id == -1)
                {
                    this.id = borrower.Id;
                    this.first_name = value;
                }
                else
                    throw new SQLiteException("Value is already use by another record");
            }
        }
        public string LastName
        {
            get { return this.last_name; }
            set
            {
                BorrowerDataObject borrower = BorrowerAccessor.Instance.Select(this.FirstName, value);
                if ((borrower == null) || (borrower.Id == this.Id))
                    this.last_name = value;
                else if (this.id == -1)
                {
                    this.id = borrower.Id;
                    this.last_name = value;
                }
                else
                    throw new SQLiteException("Value is already use by another record");
            }
        }
        #endregion

        #region Constructors
        public BorrowerDataObject()
        {
            this.id = -1;
            ChangeFirstAndLastName(string.Empty, string.Empty);
        }
        public BorrowerDataObject(string first_name, string last_name)
        {
            this.id = -1;
            ChangeFirstAndLastName(first_name, last_name);
        }
        public BorrowerDataObject(int id, string first_name, string last_name)
        {
            this.id = id;
            ChangeFirstAndLastName(first_name, last_name);
        }
        #endregion

        public void ChangeFirstAndLastName(string first_name, string last_name)
        {
            BorrowerDataObject borrower = BorrowerAccessor.Instance.Select(first_name, last_name);
            if ((borrower == null) || (borrower.Id == this.Id))
            {
                this.first_name = first_name;
                this.last_name = last_name;
            }
            else if (this.id == -1)
            {
                this.id = borrower.Id;
                this.first_name = first_name;
                this.last_name = last_name;
            }
            else
                throw new SQLiteException("Value is already use by another record");
        }

        public BorrowerDataObject Copy()
        {
            return new BorrowerDataObject(this.Id, this.FirstName, this.LastName);
        }

        public void Update()
        {
            if (this.id == -1)
                this.id = BorrowerAccessor.Instance.Insert(this.FirstName, this.LastName);
            else
                BorrowerAccessor.Instance.Update(this.Id, this.FirstName, this.LastName);
        }

        public void Delete()
        {
            if (this.id != -1)
            {
                BorrowerAccessor.Instance.Delete(this.Id);
                this.id = -1;
            }
        }

        public static BorrowerDataObject GetBorrowerDataObject(int id)
        {
            return BorrowerAccessor.Instance.Select(id);
        }
    }
}
